package com.du.mq.controller;

import com.du.common.base.model.ResBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
 * @Desc :
 * @Author: shuangdu
 * @date : 2022/5/9
 */
@RestController
@Slf4j
public class MqController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @PostMapping(value = "send")
    public ResBean sendMsg(@RequestParam String msg, @RequestParam String orderId) {
        GenericMessage<String> payload = new GenericMessage<>(msg);
        String des = "topic_mq:tagA";
        // 消息超时
//        SendResult result = rocketMQTemplate.syncSend(des, payload, 5 * 1000);
//        log.info("发送消息完成 {}", result);

        // 顺序发送
        for (int i = 0; i < 10; i++) {
            final int index = i;
            CompletableFuture.runAsync(() -> {
                SendResult result1 = rocketMQTemplate.syncSendOrderly(des, new GenericMessage<>(index + msg), orderId);
                log.info("发送消息完成 {}-{}", index, result1);
            });
        }

        return ResBean.ok();
    }

}
